package com.qiku.reggie.common;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> handlerException1(SQLIntegrityConstraintViolationException ex) {
        String message = ex.getMessage(); // Duplicate entry 'anke' for key 'idx-username'
        // 解析出用户名   用空格分割 取下标为2
        String s = message.split(" ")[2];
        // 响应错误提示信息
        return R.error(s + "用户名已经存在");
    }

    @ExceptionHandler(CustomerException.class)
    public R<String> handlerException2(CustomerException ex) {
        String message = ex.getMessage();
        // 响应错误提示信息
        return R.error(message);
    }




}
